package com.tedu._04weibo.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tedu._04weibo.base.response.JsonResult;
import com.tedu._04weibo.base.response.StatusCode;
import com.tedu._04weibo.mapper.WeiboMapper;
import com.tedu._04weibo.pojo.dto.WeiboAddParam;
import com.tedu._04weibo.pojo.entity.Weibo;
import com.tedu._04weibo.pojo.vo.UserVO;
import com.tedu._04weibo.pojo.vo.WeiboDetailVO;
import com.tedu._04weibo.pojo.vo.WeiboIndexVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.hibernate.validator.constraints.Range;


import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("v1/weibo")
@Tag(name = "2-微博模块",description = "2-微博模块")
@Validated
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @PostMapping("/insert")
    @Operation(summary = "微博发布")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(@RequestBody WeiboAddParam weiboAddParam, HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        Weibo weibo = new Weibo();
        weibo.setContent(weiboAddParam.getContent());
        weibo.setUserId(userVO.getId());
        weibo.setCreateTime(new Date());
        int num = weiboMapper.insert(weibo);
        return num>0?JsonResult.success():new JsonResult(StatusCode.OPERATION_FAILED);
    }
    @GetMapping("/select")
    @Operation(summary = "微博列表")
    @ApiOperationSupport(order=20)
    public JsonResult select(){
        List<WeiboIndexVO> weiboIndexVO = weiboMapper.select();
        return JsonResult.success(weiboIndexVO);
    }
    @GetMapping("/selectById")
    @Operation(summary = "微博详情")
    @ApiOperationSupport(order = 30)
    public JsonResult selectDetail(@Range(min = 200,max=500,message = "编号id必须在200-500之间")
                                       Long id){
        WeiboDetailVO detailVO = weiboMapper.selectById(id);
        return JsonResult.success(detailVO);
    }
}
